package cn.com.eth.yx.ethpro.util;//package cn.com.jdyun.util;


import org.web3j.crypto.Bip39Wallet;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.http.HttpService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
public class EthUtil {
    private static final String URL = "http://127.0.0.1:8545/";
    public static final String KEYSTOREPATH = "C:\\Users\\Administrator\\AppData\\Roaming\\Ethereum\\keystore";
    public static final String PASSWORD = "yx2018";

    /**
     * create eth wallet
     *
     * @throws CipherException
     * @throws IOException
     */
    public static String CreateWallet() throws CipherException, IOException {

        Bip39Wallet wallet = WalletUtils.generateBip39Wallet(PASSWORD, new File(KEYSTOREPATH));

        String fileName = wallet.getFilename();
        return getAddress(fileName);
    }


    public static String getAddress(String fileName) {

        String walletAddress = "0x" + fileName.substring(
                fileName.lastIndexOf("-") + 1,
                fileName.lastIndexOf("-") + 41);
        return walletAddress;
    }

    public static List<File> IterateAllWallet(String path) {

        List<File> fileList = new ArrayList<File>();
        File file = new File(path);
        File[] files = file.listFiles();
        if (files == null) {
            return fileList;
        }
        for (File f : files) {
            if (f.isFile()) {
                fileList.add(f);
            } else if (f.isDirectory()) {
                System.out.println(f.getAbsolutePath());
                IterateAllWallet(f.getAbsolutePath());
            }
        }
//		for (File f:fileList){
//			System.out.println(getAddress(f.getName()));
//		}
        return fileList;
    }

    public static File getFileFromAddress(String address) {

        File file = new File(KEYSTOREPATH);
        if (!file.isDirectory() || file.listFiles().length == 0) {
            return null;
        }

        for (File f : file.listFiles()) {
            String fileName = f.getName();
            String walletAddress = "0x" + fileName.substring(fileName.lastIndexOf("-") + 1,
                    fileName.lastIndexOf("-") + 41);
            if (address.equalsIgnoreCase(walletAddress)) {
                return f;
            }
        }
        return null;
    }


    /**
     * 连接eth
     *
     * @return
     */
    public static Web3j getConnection() {
        Web3j web3 = Web3j.build(new HttpService(URL));
        return web3;
    }

    /**
     * 初始化admin级别操作的对象
     *
     * @return Admin
     */
    public static Admin initAdmin() {
        return Admin.build(getService());
    }

    /**
     * 通过http连接到geth节点
     *
     * @return
     */
    private static HttpService getService() {
        return new HttpService(URL);
    }

}
